/**
 * Created by shenhai on 15/9/16.
 */

angular.module('mis.userAuth.controllers', [])
    .controller('userAuthCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {


        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/appUser/userAuthList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.authList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.authList = null;
            },
            params: {}
        });

        //搜索
        $scope.search = function (temp) {
            var current = temp.target;

            if (current) {
                $scope.currentPage = $scope.pagination.currentPage;
            }else {
                $scope.pagination.currentPage = 1;
                $scope.currentPage = 1;
            }

            var community = $scope.cascading.getVillageId(document.getElementById("search-target"));
            var nickName = $scope.desc;

            var body = {};
            if (community) body.community = community;
            if (nickName) body.nickName = nickName;

            if ($scope.authStatus == 1) {
                body.status = false;
                body.isValid = true;
            }else if ($scope.authStatus == 2){
                body.status = true;
                body.isValid = true;
            }else if ($scope.authStatus == 3){
                body.isValid = false;
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/userAuthList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.authList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;
            }).error(function (err) {
                $scope.authList = null;
                alert(err);
            });

        };


        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };


        $scope.handleAuth = function (auth) {

            var body = {};
            if (auth.status) {
                body = {
                    "user": auth.user.objId,
                    "community": auth.community.objId,
                    "status": false
                }
            } else {
                body = {
                    "user": auth.user.objId,
                    "community": auth.community.objId,
                    "status": true,
                }
            }
            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/auth', body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {
                    auth.status = auth.status ? false:true;
                    if (!auth.status) auth.isValid = true;
                } else {
                    alert(data.result.message)
                }
            }).error(function () {
                alert(data.result.message)
            });
        }


        // 拒绝
        $scope.refuse = function (auth, index) {

            var body = {id: auth.objId, userId:auth.user.objId, community:auth.community.objId};

            var reason =  document.getElementById('doc-modal-reason-'+index).innerHTML;
            if (reason) body.reason = reason;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/refuseAuth', body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {

                    $('#doc-modal-'+index).modal("close");
                    $('#doc-modal-reason-'+index).modal("close");
                    $scope.search(true);

                } else {
                    alert(data.result.message)
                }
            }).error(function () {
                alert(data.result.message)
            });
        };





        $scope.clickBigPic = function (imgView, auth) {
            var imgView = temp.target;

            if (imgView.src == auth.imageURL) {
                imgView.src = auth.imageURL + '?imageView2/1/w/120/h/120/q/75/format/webp';
            }else {
                imgView.src = auth.imageURL;
            }
        };



        //级联框
        var cascadData = {
            provinceUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionPrivince',
            cityUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCity',
            cityAreaUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCounty',
            villageUrl: 'http://' + backOfficeHost + '/rest/v1.0/mis/community/communityList',
            ajax: $http
        };
        $scope.cascading = new gObj.cascading(cascadData);
        $scope.cascading.paint($(".city-cascading")[0],true);

    }]);